package com.ruoyi.rescue.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.rescue.api.domain.CheRescue;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.user.api.RemoteCarService;
import com.ruoyi.user.api.domain.CheCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.rescue.service.ICheRescueService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 救援记录Controller
 * 
 * @author ruoyi
 * @date 2025-01-03
 */
@RestController
@RequestMapping("/rescue")
public class CheRescueController extends BaseController
{
    @Autowired
    private ICheRescueService cheRescueService;


    @Autowired
    private RemoteCarService remoteCarService;

    /**
     * 查询救援记录列表
     */
    @RequiresPermissions("rescue:rescue:list")
    @GetMapping("/list")
    public TableDataInfo list(CheRescue cheRescue)
    {
        startPage();
        List<CheRescue> list = cheRescueService.selectCheRescueList(cheRescue);
        return getDataTable(list);
    }

    /**
     * 导出救援记录列表
     */
    @RequiresPermissions("rescue:rescue:export")
    @Log(title = "救援记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheRescue cheRescue)
    {
        List<CheRescue> list = cheRescueService.selectCheRescueList(cheRescue);
        ExcelUtil<CheRescue> util = new ExcelUtil<CheRescue>(CheRescue.class);
        util.exportExcel(response, list, "救援记录数据");
    }

    /**
     * 获取救援记录详细信息
     */
    @RequiresPermissions("rescue:rescue:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheRescueService.selectCheRescueById(id));
    }

    /**
     * 新增救援记录
     */
    @RequiresPermissions("rescue:rescue:add")
    @Log(title = "救援记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheRescue cheRescue)
    {


        String carNumber = cheRescue.getCheCar().getCarNumber();

        AjaxResult listbychepai = remoteCarService.listbychepai(carNumber);

        Object o = listbychepai.get(AjaxResult.DATA_TAG);
        Object json = JSON.toJSON(o);
        if (json != null && !json.equals("")) {
            CheCar cheCar = JSON.parseObject(json.toString(), CheCar.class);

            Long id = cheCar.getId();
            cheRescue.setCarId(id);


        }

        return toAjax(cheRescueService.insertCheRescue(cheRescue));







    }

    /**
     * 修改救援记录
     */
    @RequiresPermissions("rescue:rescue:edit")
    @Log(title = "救援记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheRescue cheRescue)
    {
        return toAjax(cheRescueService.updateCheRescue(cheRescue));
    }

    /**
     * 删除救援记录
     */
    @RequiresPermissions("rescue:rescue:remove")
    @Log(title = "救援记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheRescueService.deleteCheRescueByIds(ids));
    }
}
